<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料计划编号" prop="sn" label-width="55">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入物料计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:mater_plan:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="mater_planList" border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" prop="number" width="90"/>
      <el-table-column label="计划状态" align="center" prop="tranState" width="90">
      <template v-slot = "scope">
      <dict-tag :options="dict.type.material_state" :value="scope.row.tranState"/>
    </template>
      </el-table-column>
      <el-table-column label="物料计划编号" align="center" prop="sn" />
      <el-table-column label="销售订单编号" align="center" prop="orderSn" />
      <el-table-column label="销售订单名称" align="center" prop="orderName" />
      <el-table-column label="生产计划编号" align="center" prop="planSn" />
      <el-table-column label="产品编码" align="center" prop="proSn" />
      <el-table-column label="产品名称" align="center" prop="proName" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.$index)"
            v-hasPermi="['mes:mater_plan:edit']"
          >生成采购需求</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:mater_plan:remove']"
          >撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 生成采购需求单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-row class="product-info-row" v-if="reqOrderPlanList && reqOrderPlanList.length > 0">
        <el-col :span="12">产品名称：{{ reqOrderPlanList[0].proName }}</el-col>
        <el-col :span="12">产品编码：{{ reqOrderPlanList[0].proSn }}</el-col>
      </el-row>
      <el-divider>物料列表</el-divider><!--分割线-->
      <el-table style="border-radius: 15px;" v-loading="loading" border :data="reqOrderPlanList" >
        <el-table-column label="物料名称" prop="matName" min-width="60" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.proMateriel.matName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="物料编码" prop="matSn" min-width="80" align="center" >
          <template slot-scope="scope">
            <span>{{scope.row.proMateriel.matSn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="需求数量" prop="matNum" min-width="40" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.matNum}}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划截止日期" prop="endDate" min-width="80" align="center">
          <template slot-scope="scope">
            <el-date-picker v-model="scope.row.endDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择截止日期"
                            style="width: 100%;">
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="采购数量" align="center" min-width="90" >
          <template slot-scope="scope">
            <el-input-number id="inputNumber" v-model:value="scope.row.reqNum" :min="0" :precision="2"/>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    &lt;!&ndash; 查看物料计划详情对话框 &ndash;&gt;-->
<!--    <el-dialog :title="title1" :visible.sync="open1" width="1200px" append-to-body>-->
<!--      <el-card>-->
<!--        <el-table v-loading="loading" :data="matDetailList" @selection-change="handleSelectionChange">-->
<!--          <el-table-column type="selection" width="55" align="center" />-->
<!--          <el-table-column label="物料名称" align="center" prop="matName" />-->
<!--          <el-table-column label="物料编号" align="center" prop="matSn" />-->
<!--          <el-table-column label="产品编号" align="center" prop="proSn" />-->
<!--          <el-table-column label="产品名称" align="center" prop="proName" />-->
<!--          <el-table-column label="生产计划编号" align="center" prop="planSn" :width="200"/>-->
<!--          <el-table-column label="需求数量" align="center" prop="reqNum" />-->
<!--          <el-table-column label="下达数量" align="center" prop="tranNum" />-->
<!--          <el-table-column label="计划开始日期" prop="planStartDate" width="250px">&ndash;&gt;-->
<!--            <template slot-scope="scope">-->
<!--&lt;!&ndash;              <el-date-picker clearable&ndash;&gt;-->
<!--&lt;!&ndash;                              v-model="scope.row.planStartDate"&ndash;&gt;-->
<!--&lt;!&ndash;                              type="date"&ndash;&gt;-->
<!--&lt;!&ndash;                              value-format="yyyy-MM-dd"&ndash;&gt;-->
<!--&lt;!&ndash;                              placeholder="请选择计划开始日期"&ndash;&gt;-->
<!--&lt;!&ndash;                              :disabled="true">&ndash;&gt;-->
<!--&lt;!&ndash;              </el-date-picker>&ndash;&gt;-->
<!--              <span>{{scope.row.planStartDate}}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="计划截止日期" prop="planEndDate" width="250px">-->
<!--            <template slot-scope="scope">-->
<!--&lt;!&ndash;              <el-date-picker clearable&ndash;&gt;-->
<!--&lt;!&ndash;                              v-model="scope.row.planEndDate"&ndash;&gt;-->
<!--&lt;!&ndash;                              type="date"&ndash;&gt;-->
<!--&lt;!&ndash;                              value-format="yyyy-MM-dd"&ndash;&gt;-->
<!--&lt;!&ndash;                              placeholder="请选择计划截止日期"&ndash;&gt;-->
<!--&lt;!&ndash;                              :disabled="true">&ndash;&gt;-->
<!--&lt;!&ndash;              </el-date-picker>&ndash;&gt;-->
<!--              <span>{{scope.row.planEndDate}}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
<!--        <div slot="footer" class="dialog-footer">-->
<!--          <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--          <el-button @click="cancel">取 消</el-button>-->
<!--        </div>-->
<!--      </el-card>-->
<!--    </el-dialog>-->

  </div>

</template>

<script>
import {
  listMater_plan,
  getMater_plan,
  delMater_plan,
  addMater_plan,
  updateMater_plan,
  newListMater_plan,
  getReqMater, addMatRequirement
} from "@/api/mes/mater_plan";
import {addRequirement} from "@/api/mes/requirement";
import Vue from "vue";

export default {
  name: "Mater_plan",
  dicts:["material_state"],
  data() {
    return {
      //存储当前数据行数
      Index:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料计划表格数据
      mater_planList: [],
      //物料计划明细表格数据
      matDetailList:[],
      //采购需求单列表
      reqOrderPlanList:[],
      //新增采购需求单
      reqMatAddList:[],
      //物料计划详情
      // 生成采购需求单弹出层标题
      title: "",
      // 查看详情弹出层标题
      title1:"",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        sn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sn: [
          { required: true, message: "物料计划编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    // matchang(option, index) {
    //   let ss = this.proList.find(item => item.matId == option)
    //   this.form.reqOrderPlanList[index].matSn= ss.matSn;
    //
    // },
    getReqMat(index){
      const proId = this.mater_planList[index].proId;
      getReqMater(proId).then(response => {
        this.reqOrderPlanList = response;
        console.log("this.reqOrderPlanList",this.reqOrderPlanList)
      });
    },
    /** 查询物料计划列表 */
    getList() {
      this.loading = true;
      newListMater_plan(this.queryParams).then(response => {
        this.mater_planList = response.rows;
        console.log("this.mater_planList",this.mater_planList)
        this.total = response.total;
        this.loading = false;
        this.calculateIndex()
      });
    },
    /** 计算列表编号 */
    calculateIndex() {
      this.mater_planList.forEach((item, index) => {
        this.$set(item, 'number', index + 1);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        sn: null,
        tranDate: null,
        tranNum: null,
        tranState: null,
        planStartDate: null,
        planEndDate: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "生成采购需求";
    },
    /** 修改按钮操作 */
    handleUpdate(row,index) {
      this.reset();
      const id = row.id || this.ids
      getMater_plan(id).then(response => {
        this.form = response.data;
        this.getReqMat(index);
        this.Index=index;
        this.open = true;
        this.title = "生成采购需求";
      });
    },
    // /** 修改按钮操作 */
    // viewTheDetails(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getMater_plan(id).then(response => {
    //     this.form = response.data;
    //     this.open1 = true;
    //     this.title1 = "物料计划明细";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.reqOrderPlanList.forEach((item, index) => {
        Vue.set(this.reqMatAddList, index, {
          orderId: this.mater_planList[this.Index].orderId,
          proId:this.mater_planList[this.Index].proId,
          bomId:item.bomId,
          matId:item.matId,
          reqNum:item.reqNum,
          planEndDate:item.endDate,
          matPlanId:this.mater_planList[this.Index].id,
        });
      });
      // console.log("this.reqMatAddList",this.reqMatAddList)
      addMatRequirement(this.reqMatAddList).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.reqMatAddList = [];
        this.open = false;
        this.getList();
      });
    },
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateMater_plan(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addMater_plan(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const sn = row.sn || this.sn;
      this.$modal.confirm('是否撤回物料计划编号为"' + sn + '"的数据项？').then(function() {
        return delMater_plan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤回成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/mater_plan/export', {
        ...this.queryParams
      }, `mater_plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
